package com.lzxzjy.serivce.impl;

import com.lzxzjy.mapper.BookMapper;
import com.lzxzjy.pojo.Book;
import com.lzxzjy.pojo.PageBean;
import com.lzxzjy.serivce.BookService;
import com.lzxzjy.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class BookServiceImpl implements BookService {
    // 获取工厂对象
    private final SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    /**
     * 添加书籍
     */
    @Override
    public boolean insertBook(Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Integer integer = bookMapper.insertBook(book);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    /**
     * 批量删除书籍
     * @param bookIds
     * @return
     */
    @Override
    public boolean deleteBookByIds(int[] bookIds) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Integer integer = bookMapper.deleteBookByBookIds(bookIds);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    /**
     * 删除书籍
     * @param bookId
     * @return
     */
    @Override
    public boolean deleteBookById(int bookId) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Integer integer = bookMapper.deleteBookByBookId(bookId);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    /**
     * 修改书籍
     * @param book
     * @return
     */
    @Override
    public boolean updateBookById(Book book) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        Integer integer = bookMapper.updateBookById(book);
        sqlSession.commit();
        sqlSession.close();
        return integer != 0;
    }

    /**
     * 查询所有书籍
     *
     * @return
     */
    @Override
    public List<Book> selectAllBook() {
        // 获取 SQLSession 对象
        SqlSession sqlSession = factory.openSession();
        // 获取对应 Mapper 对象
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        // 调用方法
        List<Book> books = bookMapper.selectAllBook();
        // 释放资源
        sqlSession.close();
        return books;
    }

    /**
     * 通过页数和每页显示的条数来进行分页查询
     *
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Book> selectBookByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);

        // 计算索引
        int begin = (currentPage - 1) * pageSize;

        List<Book> books = bookMapper.selectBookByPage(begin, pageSize);
        int totalCount = bookMapper.selectBookTotalCount();
        PageBean<Book> bookPageBean = new PageBean<>();
        bookPageBean.setRows(books);
        bookPageBean.setTotalCount(totalCount);

        sqlSession.close();
        return bookPageBean;
    }

    /**
     * 通过页数，每页显示的条数和条件来进行分布条件查询
     *
     * @param book
     * @param currentPage
     * @param pageSize
     * @return
     */
    @Override
    public PageBean<Book> selectBookByPageAndTerms(Book book, int currentPage, int pageSize) {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        // 计算索引
        int begin = (currentPage - 1) * pageSize;

        List<Book> books = bookMapper.selectBookByPageAndTerms(book, begin, pageSize);
        int totalCount = bookMapper.selectBookTotalCountByTerms(book);
        PageBean<Book> bookPageBean = new PageBean<>();
        bookPageBean.setRows(books);
        bookPageBean.setTotalCount(totalCount);

        sqlSession.close();
        return bookPageBean;
    }

    /**
     * 查询所有书籍的类型
     *
     * @return
     */
    @Override
    public List<String> selectAllBookType() {
        SqlSession sqlSession = factory.openSession();
        BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
        List<String> bookType = bookMapper.selectAllBookType();
        sqlSession.close();
        return bookType;
    }



}
